
package utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;

public class MyConnection {
    
    // Index for received answer command and message
    public static final int COMMAND = 0;
    public static final int MESSAGE = 1;
    
    public static String[] receive(ObjectInputStream in) {
        String[] receivedSeparated = new String[2];
        String receivedTotal;
        
        try {
            receivedTotal = String.valueOf(in.readObject());
        } catch (IOException | ClassNotFoundException e) {
            receivedTotal = Command.EXIT;
        }
        
        receivedSeparated[0] = receivedTotal.substring(0, 3);
        if (receivedTotal.length() > 3) {
            receivedSeparated[1] = receivedTotal.substring(3);
        } else {
            receivedSeparated[1] = "";
        }
        return receivedSeparated;
    }
    
    public static void copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
    }
}
